﻿Public Class CheckOut

    Dim chid As Integer = 0
    Private Sub browsecheckin()
        Dim frm As New ViewCheckIn()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim row = frm.DataGridView1.SelectedRows(0)
            Dim id = row.Cells("CheckInId").Value

            'MessageBox.Show(id)
            Dim dt As New DataTable
            cmd.Parameters("cid") = id
            dt = cmd.ExecuteDataTable("SELECT * FROM CheckIn WHERE Active=1 And CheckInId=@cid")
            Dim trow = dt.Rows(0)
            Me.txtcheckNo.Text = dt.Rows(0).Item("CheckInNo")
            Me.dtpin.Value = trow("DateIn")
            Me.dtpout.Value = trow("DateOut")
            Me.cboctype.SelectedValue = trow("ReservationTypeId")
            Me.cbocus.SelectedValue = trow("ClientId")
            Me.txttroom.Text = trow("TotalRoom")
            Me.txttotal.Text = trow("TotalAmount")
            Me.txtadult.Text = trow("Adult")
            Me.txtchild.Text = trow("Children")

            Dim sql = <sql>
                            SELECT GuestName,RoomNo,rt.RoomType,r.RoomPrice
                            FROM CheckInDetail cd
                            INNER JOIN Guest g on g.GuestId=cd.GuestId
                            INNER JOIN Room r on r.RoomId=cd.RoomId
                            INNER JOIN RoomType rt on rt.RoomTypeId=r.RoomTypeId
                            Where cd.Active=1 AND cd.CheckInId=@cid
                      </sql>
            cmd.Parameters("@cid") = id
            DataGridView2.DataSource = cmd.ExecuteDataTable(sql)

            'cmd.Parameters("@cid") = id
            'Dim pm = cmd.ExecuteScalar("SELECT Count(*) FROM Payment WHERE Active=1 AND CheckInId=@cid")
            'If pm > 0 Then
            '    Dim tt As New DataTable
            '    cmd.Parameters("@cid") = id
            '    tt = cmd.ExecuteDataTable("SELECT * FROM Payment Where active=1 And CheckInId=@cid")
            '    Dim ttr = tt.Rows(0)
            '    txtpayno.Text = ttr("PaymentNo")
            '    txtamount.Text = ttr("PayAmount")
            '    cbopaymethod.SelectedValue = ttr("PaymentMethodId")
            '    txtdetail.Text = ttr("PaymentDetail")
            'End If

            chid = id

        End If
    End Sub

   
    Private Sub btncheckinlist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncheckinlist.Click
        browsecheckin()
        loadclientinfo()
    End Sub
    Private Sub unenable()
        txtadr.Enabled = False
        txtcno.Enabled = False
        txtmail.Enabled = False
        txtphone.Enabled = False
        txtadult.Enabled = False
        txtcheckNo.Enabled = False
        txtchild.Enabled = False
        txttotal.Enabled = False
        txttotal.Enabled = False
        txttroom.Enabled = False
        dtpin.Enabled = False
        dtpout.Enabled = False
        cboctype.Enabled = False
        cbocus.Enabled = False
        cbosex.Enabled = False
        cbotype.Enabled = False
        dtpdatebirth.Enabled = False
    End Sub
    Private Sub CheckOut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView2)
        loadclient()
        loadctype()
        unenable()
        loadClientType()
        loadSex()
    End Sub

    Private Sub loadclient()
        cbocus.DisplayMember = "ClientName"
        cbocus.ValueMember = "ClientId"
        cbocus.DataSource = cmd.ExecuteDataTable("SELECT ClientName,ClientId FROM Client Where Active=1")
    End Sub

    Private Sub loadctype()
        cboctype.DisplayMember = "ReservationType"
        cboctype.ValueMember = "ReservationTypeId"
        cboctype.DataSource = cmd.ExecuteDataTable("SELECT reservationTypeId,ReservationType FROM ReservationType WHERE Active=1")
    End Sub
    Private Sub loadclientinfo()
        cmd.Parameters("@cid") = cbocus.SelectedValue
        Dim dt As New DataTable
        dt = cmd.ExecuteDataTable("SELECT * FROM Client WHERE Active=1 AND ClientId=@cid")
        Dim row = dt.Rows(0)
        txtcno.Text = row("ClientNo")
        txtadr.Text = row("ClientAddress")
        txtmail.Text = row("ClientMail")
        txtphone.Text = row("ClientMobilePhone")
        dtpdatebirth.Value = row("ClientDob")
        cbotype.SelectedValue = row("ClientTypeId")
        cbosex.SelectedValue = row("SexId")
    End Sub
    Private Sub loadSex()
        cbosex.DisplayMember = "SexName"
        cbosex.ValueMember = "SexId"
        cbosex.DataSource = cmd.ExecuteDataTable("SELECT * FROM [Sex] Where Active=1")
    End Sub
    Private Sub loadClientType()
        cbotype.DisplayMember = "ClientTypeName"
        cbotype.ValueMember = "ClientTypeId"
        cbotype.DataSource = cmd.ExecuteDataTable("SELECT ClientTypeName, ClientTypeId FROm ClientType Where Active=1")
    End Sub

    Private Sub btncheckout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncheckout.Click
        Try
            cmd.Begin()
            cmd.Parameters("@cid") = chid
            cmd.Parameters("@date") = Now.Date
            cmd.ExecuteNonQuery("INSERT INTO CheckOut(CheckInId,CheckOutDate,Active) Values(@cid,@date,1)")
            cmd.Commit()
            MessageBox.Show("CheckOut Sucess")
            Close()
        Catch ex As Exception
            cmd.Rollback()
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
        Close()
    End Sub
End Class